#include<stdio.h>

#define MAX_TERM 80

typedef struct juzhen
{   	  	   
    int row;        //行
    int col;        //列
    int value;        //元素值
}   	  	   ;


struct juzhen a[MAX_TERM];        //存放矩阵中元素数值不为零的元素
struct juzhen b[MAX_TERM];        //转置后的矩阵


void show(struct juzhen a[],int count_a)            //显示稀疏矩阵方法二
{   	  	   
    int i;
    printf(" i row col val\n");
    for(i = 1;i < count_a + 1;i++)
    {   	  	   
        printf(" %d|  %d   %d   %d\n",i,a[i].row,a[i].col,a[i].value);
    }
}   	  	   



void zhuanzhi(struct juzhen a[MAX_TERM],struct juzhen b[MAX_TERM])
{   	  	   
    int term[MAX_TERM],star[MAX_TERM];
    int n = a[0].value;
    int i,j,k;
    int b_star;

    for(i = 0;i < MAX_TERM;i++)
        term[i] = 0;

    for(j = 0;j <= a[0].value;j++)
        term[a[j].col]++;

    star[0] = 1;
    for(k = 1;k < MAX_TERM;k++)
        star[k] = star[k - 1] + term[k - 1];

    b[0].col = a[0].col;
    b[0].row = a[0].row;
    b[0].value = a[0].value;
    for(i = 1;i <= n;i++)
    {   	  	   
        b_star = star[a[i].col]++;
        b[b_star].col = a[i].row;
        b[b_star].row = a[i].col;
        b[b_star].value = a[i].value;
    }

}   	  	   


void init(struct juzhen a[],int rows, int cols,int count)            //初始化稀疏矩阵
{   	  	   
    int row,col,value,i;
    for ( i = 1; i < count+1; ++i)
    {   	  	   
         printf("please input the row,col and value\n");
         scanf("%d,%d,%d",&row,&col,&value);
         a[i].row = row;
         a[i].col = col;
         a[i].value = value;
    }
    a[0].row = rows;
    a[0].col = cols;
    a[0].value = count;
}   	  	   

int main(void)
{   	  	   
    int count_a,rows,cols;
    printf("please input the number of rows,cols and values\n");
    scanf("%d,%d,%d",&rows,&cols,&count_a);
    init(a,rows,cols,count_a);
    show(a,count_a);
    printf("\n");
    zhuanzhi(a,b);
    show(b,count_a);
}